package com.example.qxfw.common.controller;

import com.example.qxfw.common.service.FileRequestService;
import com.google.common.collect.Maps;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.*;

@Controller
@RequestMapping("/dataservice")
public class FileRequsetController {

    FileRequestService ms = new FileRequestService();

    @RequestMapping("/fileDataService")
    @ResponseBody
    public Object processRequest(HttpServletRequest request, HttpServletResponse response) throws UnsupportedEncodingException {
        String collectionName = "";
        CommonsMultipartResolver multipartResolver = null;
        Map<String, Object> updateData = Maps.newHashMap();
        //获取参数名 返回一个枚举类型
        Enumeration<String> em = request.getParameterNames();
        Map<String, Object> sqlMap = Maps.newHashMap();
        //遍历
        while (em.hasMoreElements()) {
            String name = (String) em.nextElement();
            String value = request.getParameter(name);
            if(!"sqlType".equals(name)) {
                if ("collectionName".equals(name)) {
                    collectionName = value;
                }
                //添加到集合中
                sqlMap.put(name, value);
            }
        }
        if (StringUtils.isBlank(collectionName)) {
            return "collectionName参数不能为空，请确定数据库表名称";
        }
        try {
            String sqlType = request.getParameter("sqlType");
            if ("select".equals(sqlType)) {
                byte[] result = null;
                result = ms.selectData(sqlMap);
                return result;
            }else if("insert".equals(sqlType)) {
                int result = 0;
                int count = 0;
                multipartResolver = new CommonsMultipartResolver(
                        request.getSession().getServletContext());
                if (request instanceof MultipartHttpServletRequest) {
                    // 将request变成多部分request
                    MultipartHttpServletRequest multiRequest = (MultipartHttpServletRequest) request;
                    Iterator<String> iter = multiRequest.getFileNames();
                    if (multipartResolver.isMultipart(request) && iter.hasNext()) {
                        // 获取multiRequest 中所有的文件名
                        while (iter.hasNext()) {
                            // 一次遍历所有文件
                            MultipartFile file = multiRequest.getFile(iter.next().toString());
                            if (file != null) {
                                try {
                                    sqlMap.put("data", file.getBytes());
                                } catch (IOException e) {
                                    e.printStackTrace();
                                }
                                int flag = ms.insertData(sqlMap);
                                result += flag;
                                count++;
                                return count + "";
                            }
                        }
                    }
                }
            }else if("update".equals(sqlType)){
                long updateCount = 0;
                multipartResolver = new CommonsMultipartResolver(
                        request.getSession().getServletContext());
                if (request instanceof MultipartHttpServletRequest) {
                    // 将request变成多部分request
                    MultipartHttpServletRequest multiRequest = (MultipartHttpServletRequest) request;
                    Iterator<String> iter = multiRequest.getFileNames();
                    if (multipartResolver.isMultipart(request) && iter.hasNext()) {
                        // 获取multiRequest 中所有的文件名
                        while (iter.hasNext()) {
                            // 一次遍历所有文件
                            MultipartFile file = multiRequest.getFile(iter.next().toString());
                            if (file != null) {
                                updateData.put("data", file.getBytes());
                                updateCount = ms.updateData(sqlMap, updateData);
                            }
                        }
                    }
                }
                return updateCount+ "";
            }else if("delete".equals(sqlType)){
                long deleteCount = 0;
                deleteCount = ms.deleteData(sqlMap);
                return deleteCount+ "";
            }else if("exist".equals(sqlType)){
                long existCount = 0;
                existCount = ms.existData(sqlMap);
                return existCount+ "";
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return "sqlType参数异常";
    }

}
